<template>
  <view
    v-if="show"
    class="tn-loading-class tn-loading"
    :class="[
      `tn-loading-${mode}`,
      animation ? `tn-loading-${mode}--animation` : ''
    ]"
    :style="[loadStyle]"
  ></view>
</template>

<script>
  export default {
    name: 'tn-loading',
    props: {
      // 动画类型
      // circle 圆圈 flower 花朵形状
      mode: {
        type: String,
        default: 'circle'
      },
      // 是否显示
      show: {
        type: Boolean,
        default: true
      },
      // 是否显示加载动画
      animation: {
        type: Boolean,
        default: true
      },
      // 圆圈颜色
      color: {
        type: String,
        default: ''
      },
      // 图标大小
      size: {
        type: Number,
        default: 34
      }
    },
    computed: {
      // 加载动画圆圈的样式
      loadStyle() {
        let style = {}
        style.width = this.size + 'rpx'
        style.height = style.width
        if (this.mode === 'circle') style.borderColor = `#E6E6E6 #E6E6E6 #E6E6E6 ${this.color ? this.color : '#AAAAAA'}`
        
        return style
      }
    }
  }
</script>

<style lang="scss" scoped>
  .tn-loading-circle {
    /* #ifndef APP-NVUE */
    display: inline-flex;
    /* #endif */
    vertical-align: middle;
    width: 28rpx;
    height: 28rpx;
    background: 0 0;
    border-radius: 50%;
    border: 2px solid;
    border-color: #E6E6E6 #E6E6E6 #E6E6E6 #AAAAAA;
    
    &--animation {
      animation: tn-circle 1s linear infinite;
      -webkit-animation: tn-circle 1s linear infinite;
    }
  }
  
  .tn-loading-flower {
    display: inline-block;
    vertical-align: middle;
    width: 28rpx;
    height: 28rpx;
    background: transparent url() no-repeat;;
    background-size: 100%;
    
    &--animation {
      animation: tn-flower 1s steps(12) infinite;
      -webkit-animation: tn-flower 1s steps(12) infinite;
    }
  }
  
  @keyframes tn-flower {
  	0% {
  		transform: rotate(0deg);
  		-webkit-transform: rotate(0deg);
  	}
  
  	to {
  		transform: rotate(360deg);
  		-webkit-transform: rotate(360deg);
  	}
  }
  
  @keyframes tn-circle {
  	0% {
  		transform: rotate(0);
  		-webkit-transform: rotate(0);
  	}
  
  	100% {
  		transform: rotate(360deg);
  		-webkit-transform: rotate(360deg);
  	}
  }
</style>
